//查询(搜索)问卷列表
import { useSearchParams } from "react-router-dom";
import { useRequest } from "ahooks";
import { getQuestionListService } from "../services/question";
import {
    LIST_PAGE_SIZE,
  LIST_SEARCH_PARAM_KEY,
  LIST_SEARCH_PARAM_PAGE,
  LIST_SEARCH_PARAM_PAGESIZE,
} from "../constant";

type OptionType = {
    keyword:string,
    isStar:boolean,
    isDeleted:boolean,
    page:number,
    pageSize:number,
};

function useLoadQuestionListData(opt: Partial<OptionType> = {}) {
  const { isStar, isDeleted } = opt;
  const [searchParams] = useSearchParams();

  const { data, loading, error ,refresh} = useRequest(
    async () => {
      const keyword = searchParams.get(LIST_SEARCH_PARAM_KEY) || "";
      const page =
        parseInt(searchParams.get(LIST_SEARCH_PARAM_PAGE) || "") || 1;
      const pageSize =
        parseInt(searchParams.get(LIST_SEARCH_PARAM_PAGESIZE) || "") || LIST_PAGE_SIZE;
      const data = await getQuestionListService({ keyword, isStar, isDeleted ,page,pageSize});
      return data;
    },
    {
      refreshDeps: [searchParams], //刷新的依赖项
    }
  );
  return { data, loading, error,refresh };
}

export default useLoadQuestionListData;
